package com.jrx.anytxn.investor.service.impl;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.SeqUtils;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.customer.dto.customer.CustomerFirstLevelRes;
import com.jrx.anytxn.customer.dto.customer.CustomerRes;
import com.jrx.anytxn.customer.dto.customer.CustomerSecondLevelRes;
import com.jrx.anytxn.customer.entity.CmCustomerAddressInfo;
import com.jrx.anytxn.customer.entity.CmCustomerContactInfo;
import com.jrx.anytxn.customer.mapper.ext.ExtCmCustomerAddressInfoMapper;
import com.jrx.anytxn.customer.mapper.ext.ExtCmCustomerContactInfoMapper;
import com.jrx.anytxn.investor.bean.InvestorCreditBean;
import com.jrx.anytxn.investor.constant.TransBizConstant;
import com.jrx.anytxn.investor.entity.TlCustomerInvestorLimit;
import com.jrx.anytxn.investor.entity.TlInvestorLimitApproveLog;
import com.jrx.anytxn.investor.mapper.ext.ExtTlCustomerInvestorLimitMapper;
import com.jrx.anytxn.investor.service.IInvestorCustomerLimitService;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.ribbon.config.GrayVersionConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 客户出资方授信额度Service接口 实现
 *
 * @author liuyang
 * @date 2020-06-09
 */
@Service
public class InvestorCustomerLimitServiceImpl implements IInvestorCustomerLimitService {

    private static final Logger logger = LoggerFactory.getLogger(InvestorCustomerLimitServiceImpl.class);

    @Autowired
    private ISegmentService segmentService;

    @Resource
    private ExtTlCustomerInvestorLimitMapper customerInvestorLimitMapper;

    @Resource
    private ExtCmCustomerContactInfoMapper extCmCustomerContactInfoMapper;

    @Resource
    private ExtCmCustomerAddressInfoMapper extCmCustomerAddressInfoMapper;

    @Autowired
    private GrayVersionConfig grayVersionConfig;

    /**
     * 构建客户出资方授信额度
     *
     * @param investorCreditBean 联合贷授信bean
     * @param investorId 出资方id
     * @return TlCustomerInvestorLimit 客户出资方授信额度
     * @throws TxnBizException
     * @author liuyang
     * @date 2020-06-09
     */
    @Override
    public TlCustomerInvestorLimit buildCustomerInvestorLimit(InvestorCreditBean investorCreditBean, String investorId) throws TxnBizException {
        TlCustomerInvestorLimit customerInvestorLimit = new TlCustomerInvestorLimit();
        BeanMapping.copy(investorCreditBean, customerInvestorLimit);
        try {
            customerInvestorLimit.setId(segmentService.getId(TransBizConstant.TL_CUSTOMER_INVESTOR_LIMIT));// 主键
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(TxnRespCode.ERROR.getUserTips());
        }
        customerInvestorLimit.setInvestorId(investorId);// 出资方id
        customerInvestorLimit.setCreditLimit(investorCreditBean.getLimitAmount());// 授信额度
        customerInvestorLimit.setLimitStatus(TransBizConstant.LIMIT_STATUS_1);// 状态
        customerInvestorLimit.setUpdateBy(TransBizConstant.SYS_OPRATE);// 最后更新操作员
        customerInvestorLimit.setRecordVersionNumber(TransBizConstant.INIT_RECORD_VERSION_NUMBER);// 记录版本号
        return  customerInvestorLimit;
    }



    /**
     * 构建客户出资方授信审批记录
     *
     * @param investorCreditBean 联合贷授信bean
     * @param investorId 出资方id
     * @param customerRes 客户信息
     * @param approveStatus 审批状态
     * @return TlInvestorLimitApproveLog 客户出资方授信额度
     * @throws TxnBizException
     * @author liuyang
     * @date 2020-06-09
     */
    @Override
    public TlInvestorLimitApproveLog buildInvestorLimitApproveLog(InvestorCreditBean investorCreditBean, String investorId, CustomerRes customerRes, String approveStatus) throws TxnBizException {

        CustomerFirstLevelRes firstLevel = customerRes.getFirstLevel();
        CustomerSecondLevelRes secondLevel = customerRes.getSecondLevel();
        List<CmCustomerContactInfo> contactInfoList = extCmCustomerContactInfoMapper.selectByTenantIdAndCustomerId(investorCreditBean.getTenantId(),secondLevel.getCustomerId());
        List<CmCustomerAddressInfo> addressInfoList = extCmCustomerAddressInfoMapper.selectByCustomerIdAndTenantId(secondLevel.getCustomerId(),investorCreditBean.getTenantId());

        TlInvestorLimitApproveLog investorLimitApproveLog = new TlInvestorLimitApproveLog();
        BeanMapping.copy(investorCreditBean, investorLimitApproveLog);
        try {
            investorLimitApproveLog.setId(segmentService.getId(TransBizConstant.TL_INVESTOR_LIMIT_APPROVE_LOG));// 主键
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(TxnRespCode.ERROR.getUserTips());
        }
        investorLimitApproveLog.setInvestorId(investorId);// 出资方id
        investorLimitApproveLog.setSeqNo(SeqUtils.getSN());// 授信流水号
        investorLimitApproveLog.setApproveStatus(approveStatus);// 审批状态
        investorLimitApproveLog.setCreditLimit(investorCreditBean.getLimitAmount());// 授信额度
        investorLimitApproveLog.setChineseName(investorCreditBean.getChineseName()); // 姓名
        investorLimitApproveLog.setMobile(secondLevel.getMobile());// 手机号
        investorLimitApproveLog.setIdNumber(firstLevel.getIdNumber()); // 证件号
        investorLimitApproveLog.setIdType(firstLevel.getIdType()); // 证件类型
        investorLimitApproveLog.setIdIssueAgent(firstLevel.getIdIssueAgent()); // 发证机关
        investorLimitApproveLog.setIdEffectiveDate(firstLevel.getIdEffectiveDate()); // 证件有效期起始日期
        investorLimitApproveLog.setIdExpireDate(firstLevel.getIdExpireDate()); // 证件有效期终止日期
        investorLimitApproveLog.setRace(firstLevel.getRace()); // 民族
        investorLimitApproveLog.setSex(firstLevel.getSex()); // 性别
        investorLimitApproveLog.setMaritalStatus(secondLevel.getMaritalStatus()); // 婚姻状态
        investorLimitApproveLog.setEducation(secondLevel.getEducation()); // 学历
        investorLimitApproveLog.setOccuptionType(""); // 职业类型 TODO 需要从风控获取
        investorLimitApproveLog.setCompanyName(""); // 公司名称
        investorLimitApproveLog.setRelationship(""); // 联系人关系
        investorLimitApproveLog.setContactName(""); // 联系人姓名
        investorLimitApproveLog.setContactMobile(""); // 联系人电话
        investorLimitApproveLog.setLiveAddress(""); // 住址
        investorLimitApproveLog.setGrayVersion(grayVersionConfig.getCurrentVersion());
        // 风控扩展字段 TODO 需要从风控获取
        investorLimitApproveLog.setUpdateBy(TransBizConstant.SYS_OPRATE);// 最后更新操作员
        investorLimitApproveLog.setRecordVersionNumber(TransBizConstant.INIT_RECORD_VERSION_NUMBER);// 记录版本号
        return  investorLimitApproveLog;
    }

    /**
     * 根据客户id和出资方id查询客户出资方授信额度
     *
     * @param customerId 客户id
     * @param investorId 出资方id
     * @param tenantId 租户ID
     * @param statusList 状态
     * @return TlCustomerInvestorLimit 客户出资方授信额度
     * @throws TxnBizException
     * @author liuyang
     * @date  2020-06-09
     */
    @Override
    public List<TlCustomerInvestorLimit> findByCustomerIdAndInvestorId(String customerId, String investorId, String tenantId, List<String> statusList) throws TxnBizException {
        // 入参判断
        if (StringUtils.isEmpty(customerId) || StringUtils.isEmpty(investorId) || StringUtils.isEmpty(tenantId)) {
            logger.error("客户出资方授信额度查询客户id，出资方id，租户ID不能为空");
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getCode(), TxnRespCode.NOT_EMPTY.getUserTips());
        }

        // 查询客户出资方授信额度
        List<TlCustomerInvestorLimit> customerInvestorLimits = customerInvestorLimitMapper.selectByCustomerIdAndInvestorId(customerId, investorId, tenantId, statusList);
        return  customerInvestorLimits;
    }

    /**
     * 构建客户出资方授信额度
     *
     * @param investorLimitApproveLog 授信审批记录
     * @return TlCustomerInvestorLimit 客户出资方授信额度
     * @throws TxnBizException
     * @author liuyang
     * @date 2020-06-17
     */
    @Override
    public TlCustomerInvestorLimit buildCustomerInvestorLimit(TlInvestorLimitApproveLog investorLimitApproveLog) throws TxnBizException {
        TlCustomerInvestorLimit customerInvestorLimit = new TlCustomerInvestorLimit();
        BeanMapping.copy(investorLimitApproveLog, customerInvestorLimit);
        try {
            customerInvestorLimit.setId(segmentService.getId(TransBizConstant.TL_CUSTOMER_INVESTOR_LIMIT));// 主键
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(TxnRespCode.ERROR.getUserTips());
        }
        customerInvestorLimit.setLimitStatus(TransBizConstant.LIMIT_STATUS_1);// 状态
        customerInvestorLimit.setCreateTime(null);// 创建时间
        customerInvestorLimit.setUpdateTime(null);// 更新时间
        customerInvestorLimit.setUpdateBy(TransBizConstant.SYS_OPRATE);// 最后更新操作员
        customerInvestorLimit.setRecordVersionNumber(TransBizConstant.INIT_RECORD_VERSION_NUMBER);// 记录版本号
        return  customerInvestorLimit;
    }
}
